
#include "shared_parameters/shared_params_interface.hpp"
#include <chrono>
#include <thread>
#include <iostream>
#include <cmath>
#include <csignal>

volatile sig_atomic_t stop = 0;

void signal_handler(int) {
    stop = 1;
}

struct ErrorStats {
    int int_errors = 0;
    int double_errors = 0;
    int string_errors = 0;
    int bool_errors = 0;
    int total_reads = 0;
};

int main() {
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    if (!SharedParamsInterface::initialize()) {
        std::cerr << "Reader: Failed to connect to shared memory!" << std::endl;
        return 1;
    }

    ErrorStats stats;
    int last_valid_counter = -1;

    while (!stop) {
        // 原子读取验证流程
        int counter1 = SharedParamsInterface::getParam<int>("sync", "counter", -1);
        if (counter1 == -1) continue;

        // 读取所有参数
        auto int_val = SharedParamsInterface::getParam<int>("test_node", "int_param", -1);
        auto double_val = SharedParamsInterface::getParam<double>("test_node", "double_param", -1.0);
        auto str_val = SharedParamsInterface::getParam<std::string>("test_node", "string_param", "");
        auto bool_val = SharedParamsInterface::getParam<bool>("test_node", "bool_param", false);
        
        int counter2 = SharedParamsInterface::getParam<int>("sync", "counter", -1);

        // 验证原子性
        if (counter1 != counter2 || counter1 < last_valid_counter) {
            continue; // 数据不一致，跳过本次
        }

        // 生成预期值
        std::string expected_str = "string_" + std::to_string(counter1) + "_";
        bool expected_bool = (counter1 % 2) == 0;

        // 严格验证
        bool valid = true;
        if (int_val != counter1) {
            stats.int_errors++;
            valid = false;
        }
        if (!std::regex_search(str_val, std::regex(expected_str))) {
            stats.string_errors++;
            valid = false;
        } 
        if (bool_val != expected_bool) {
            stats.bool_errors++;
            valid = false;
        }

        // 双重验证double值的有效性
        if (valid) {
            try {
                size_t underscore_pos = str_val.find_last_of('_');
                auto timestamp = std::stol(str_val.substr(underscore_pos+1));
                double expected_double = 0.1 * counter1 + timestamp/1e9;
                if (std::abs(double_val - expected_double) > 1e-6) {
                    stats.double_errors++;
                    valid = false;
                }
            } catch (...) {
                stats.string_errors++;
                valid = false;
            }
        }

        if (!valid) {
            std::cerr << "Mismatch detected at counter: " << counter1 
                     << " [int=" << int_val << " str=" << str_val << "...]" << std::endl;
        }

        stats.total_reads++;
        last_valid_counter = counter1;

        std::this_thread::sleep_for(std::chrono::milliseconds(30)); // 更快的读取频率
    }

    // 输出最终统计结果
    std::cout << "\nValidation Report:\n"
              << "Total reads: " << stats.total_reads << "\n"
              << "Int errors: " << stats.int_errors << "\n"
              << "Double errors: " << stats.double_errors << "\n"
              << "String errors: " << stats.string_errors << "\n"
              << "Bool errors: " << stats.bool_errors << "\n"
              << "Error rate: " 
              << (stats.total_reads ? 100.0*(stats.int_errors + stats.double_errors + 
                    stats.string_errors + stats.bool_errors)/stats.total_reads : 0)
              << "%" << std::endl;

    return 0;
}